To get the most out of this article, you need to
understand some background about what a file type is and how Windows 7
determines and works with file types. The next couple of sections tell
you everything you need to know to get you through the rest of the article.
File Types and File Extensions
One
of the fictions that Microsoft has tried to foist on the computer-using
public is that we live in a “document-centric” world. That is, that
people care only about the documents they create and not about the
applications they use to create those documents. This is pure hokum.
The reality is that applications are still too difficult to use and the
capability to share documents between applications is still too
problematic. In other words, you can’t create documents unless you
learn the ins and outs of an application, and you can’t share documents
with others unless you use compatible applications.
Why
does the lack of file extensions cause such a fuss? Because file
extensions determine the file type of a document. In other words, if
Windows 7 sees that a file has a .txt
extension, it concludes the file uses the Text Document file type.
Similarly, a file with the extension .bmp uses the Bitmap Image file
type.
The file type, in turn, determines the application that’s associated with the extension. If a file has a .txt
extension, Windows 7 associates that extension with Notepad, so the
file will always open in Notepad. Nothing else inherent in the file
determines the file type so, at least from the point of view of the
user, the entire Windows 7 file system rests on the scrawny shoulders
of the humble file extension.
This
method of determining file types is, no doubt, a poor design decision.
For example, there is some danger that a novice user could render a
file useless by imprudently renaming its extension. Interestingly,
Microsoft seems to have recognized this danger and programmed a subtle
behavior change into recent versions of Windows (Vista and 7): When
file extensions are turned on and you activate the Rename command
(click the file and then press F2), Windows displays the usual text box
around the entire filename, but it selects only the file’s primary name (the part to the left of the dot), as shown in Figure 1. Pressing any character obliterates the primary name, but leaves the extension intact.
Despite
the drawbacks that come with file extensions, they lead to some
powerful methods for manipulating and controlling the Windows 7 file
system, as you see in the rest of this chapter.
File Types and the Registry
As
you might expect, everything Windows 7 knows about file types is
defined in the Registry. You use the Registry to work with file types
throughout this chapter, so let’s see how things work. Open the
Registry Editor (click Start, type regedit,
press Enter, and enter your UAC credentials) and examine the
HKEY_CLASSES_ROOT key. Notice that it’s divided into two sections:
The first part of HKEY_CLASSES_ROOT consists of dozens of file extension subkeys (such as .bmp and .txt).
There are well over 400 such subkeys in a basic Windows 7 installation,
and there could easily be two or three times that number on a system
with many applications installed.
The second part of HKEY_CLASSES_ROOT
lists the various file types that are associated with the registered
extensions. When an extension is associated with a particular file
type, the extension is said to be registered with Windows 7.
Note
HKEY_CLASSES_ROOT also stores information on ActiveX controls in its CLSID subkey. Many of these controls also have corresponding subkeys in the second half of HKEY_CLASSES_ROOT.
To see what this all means, take a look at Figure 2. Here, I’ve selected the .txt key, which has txtfile as its Default value.
That Default value is a pointer to the extension’s associated file type subkey in the second half of HKEY_CLASSES_ROOT. Figure 3 shows the txtfile subkey associated with the .txt extension. Here are some notes about this file type subkey:
The Default value is a description of the file type (Text Document, in this case).
The DefaultIcon subkey defines the icon that’s displayed with any file that uses this type.
The shell
subkey determines the actions that can be performed with this file
type. These actions vary depending on the file type, but Open and Print
are common. The Open action determines the application that’s associated with the file type. For example, the Open action for a Text Document file type is the following:
%SystemRoot%\system32\NOTEPAD.EXE %1
Note
The %1
at the end of the command is a placeholder that refers to the document
being opened (if any). If you double-click a file named memo.txt, for
example, the %1memo.txt, which tells Windows to run Notepad and open that file. placeholder is replaced by